package com.thread.threadfirst.studyThread1;

import java.util.concurrent.TimeUnit;

/**
 * 9、在线程执行到过程中抛出异常了，此时该线程会释放锁，其他线程竞争到锁后会继续执行上个线程没有执行完的
 * 故在多线程中，如果出现异常了要好好处理
 */
public class T1 {
    int count = 0;

    public synchronized void m() {
        System.out.println(Thread.currentThread().getName() + " start");
        while (true) {
            count++;
            System.out.println(Thread.currentThread().getName() + " count= " + count);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count == 5) {
                int i = 1 / 0;
            }
        }
    }

    public static void main(String[] args) {
        T1 t1 = new T1();

        Runnable r = new Runnable() {

            @Override
            public void run() {
                t1.m();
            }
        };
        new Thread(r, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(r, "t2").start();
    }
}
